package com.jzdjt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.PaperEntity;
import com.jzdjt.tool.DBUtil;

import net.sf.json.JSONArray;

public class GetPaperHistoryListServlet extends HttpServlet {

    public GetPaperHistoryListServlet() {
        super();
    }

    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    	response.setContentType("text/html;charset=utf-8"); 
        response.setCharacterEncoding("UTF-8"); 
       
        //根据标示名获取JSP文件中表单所包含的参数 
        String papertype=request.getParameter("papertype");
        String subject=request.getParameter("subject"); 
        String grade=request.getParameter("grade");
        String stuid=request.getParameter("stuid");
        DBUtil dbUtil = new DBUtil(); 
        
         String  result = getPaperList(papertype,subject,grade,stuid);  
        
        
        PrintWriter out = response.getWriter();//回应请求
         
        out.write(result);
        out.flush();
        out.close();
        System.out.println(result);  
    }

    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	 doGet(request, response); 
         
        
         response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
        
         //根据标示名获取JSP文件中表单所包含的参数 
         String subject=request.getParameter("subject");
         String stuid=request.getParameter("stuid");
         String papertype=request.getParameter("papertype");
         String grade=request.getParameter("grade");
          String  result = getPaperList(papertype,subject,grade,stuid);  
         
         
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result);  
    }

	private String getPaperList(String papertype,String subject, String grade,String stuid) {
		try {
			String getPaperSql = "";
			if(subject==null||subject.trim().equals("")){
	            return null;
	        }
	        else if(subject.contains("&")){
	            String[] subjectarry= subject.split("&");
	            String subjectsql = "";
	            subjectsql = " ( PaperXK = '"+subjectarry[0]+"'";
	            for(int i=1;i<subjectarry.length;i++){
	                subjectsql += " OR PaperXK = '"+subjectarry[i]+"'";
	            }
	            subjectsql += ") ";
	            getPaperSql = "select * from PaperInfo where PaperStatue='已发布'  and "+subjectsql+" and PaperGrade='"+grade+"' and PaperID not in(select PaperID from ObjAnswer where StudentID = '"+stuid+"') order by PaperDate desc";
	        }
	        else{
	            getPaperSql = "select * from PaperInfo where PaperStatue='已发布'    and PaperXK='"+subject+"' and PaperGrade='"+grade+"' and PaperID not in(select PaperID from ObjAnswer where StudentID = '"+stuid+"') order by PaperDate desc";
	        }
	        DBUtil dbUtil = new DBUtil();
	        List<PaperEntity> videolist = dbUtil.QueryFromPaper(getPaperSql);
	        JSONArray array =  JSONArray.fromObject(videolist);
	        return array.toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "";
		}
		
	}

}